package com.shujia.source

import java.util.Properties

import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer

object Demo4kafkaSource {

  def main(args: Array[String]): Unit = {

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val properties = new Properties()
    properties.setProperty("bootstrap.servers","master:9092")
    properties.setProperty("group.id","test")

    //创建消费者
    val flinkKafkaConsumer = new FlinkKafkaConsumer[String](
      "test_topic1",
      new SimpleStringSchema(),
      properties
    )

    flinkKafkaConsumer.setStartFromEarliest() // 尽可能从最早的记录开始
    //flinkKafkaConsumer.setStartFromLatest()        // 从最新的记录开始
    //flinkKafkaConsumer.setStartFromTimestamp(...)  // 从指定的时间开始（毫秒）
    //flinkKafkaConsumer.setStartFromGroupOffsets()  // 默认的方法

    val kafkaDS: DataStream[String] = env.addSource(flinkKafkaConsumer)

    kafkaDS.print()

    env.execute()

  }

}
